﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Logs.Core;

namespace Pixysoft.Framework.Logs
{
    /// <summary>
    /// 为version / datamining 做准备
    /// 
    /// 添加项目
    /// 归档
    /// 打包
    /// 
    /// 默认属性：date/id
    /// 
    /// 基于时间的搜索、基于id的搜索
    /// </summary>
    public class RecordManager
    {
        /// <summary>
        /// 输入目标文件绝对地址，查询记录
        /// </summary>
        /// <param name="fileFullPathName"></param>
        /// <param name="elementName"></param>
        /// <returns></returns>
        public static IRecordCommand CreateCommand(string fileFullPathName,
            string elementName)
        {
            string path = Pixysoft.StringFilter.GetFilePath(fileFullPathName);

            string filename = Pixysoft.StringFilter.GetFileName(fileFullPathName);

            string postfix = Pixysoft.StringFilter.GetFileSuffix(fileFullPathName);

            return CreateCommand(path, filename, postfix, elementName, false);
        }

        /// <summary>
        /// 输入路径、文件名、后缀、节点名，查询节点
        /// </summary>
        /// <param name="path"></param>
        /// <param name="filename"></param>
        /// <param name="postfix"></param>
        /// <param name="elementName"></param>
        /// <returns></returns>
        public static IRecordCommand CreateCommand(string path,
            string filename, string postfix, string elementName)
        {
            return CreateCommand(path, filename, postfix, elementName, false);
        }

        /// <summary>
        /// 输入路径、文件名、后缀、节点名，查询节点
        /// </summary>
        /// <param name="path"></param>
        /// <param name="filename"></param>
        /// <param name="postfix"></param>
        /// <param name="crossFiles"></param>
        /// <returns></returns>
        public static IRecordCommand CreateCommand(string path,
            string filename, string postfix, string elementName, bool crossFiles)
        {
            if (string.IsNullOrEmpty(path))
                throw new Exception("missing path");

            if (string.IsNullOrEmpty(filename))
                throw new Exception("missing file");

            if (string.IsNullOrEmpty(postfix))
                throw new Exception("missing postfix");

            if (string.IsNullOrEmpty(elementName))
                throw new Exception("missing elementName");

            if (!path.EndsWith(@"\"))
                path += @"\";

            if (!postfix.StartsWith("."))
                postfix = "." + postfix;

            return new RecordCommand(path, filename, postfix, elementName, crossFiles);

        }

        /// <summary>
        /// 创建一个记录器
        /// </summary>
        /// <param name="path"></param>
        /// <param name="file"></param>
        /// <param name="postfix"></param>
        /// <returns></returns>
        public static IRecorder CreateRecorder(string path,
            string file, string postfix)
        {
            if (string.IsNullOrEmpty(path))
                throw new Exception("missing path");

            if (string.IsNullOrEmpty(file))
                throw new Exception("missing file");

            if (string.IsNullOrEmpty(postfix))
                throw new Exception("missing postfix");

            if (!path.EndsWith(@"\"))
                path = path + @"\";

            if (!postfix.StartsWith("."))
                postfix = "." + postfix;

            return new Recorder(path, file, postfix);
        }

        /// <summary>
        /// 全打包 包括正在使用没有归档的文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="file"></param>
        /// <param name="postfix"></param>
        /// <returns></returns>
        public static string CreatePackage(string path,
            string file, string postfix)
        {
            return CreatePackage(path, file, postfix, -1);
        }

        /// <summary>
        /// 打包
        /// </summary>
        /// <param name="path"></param>
        /// <param name="file"></param>
        /// <param name="postfix"></param>
        /// <returns></returns>
        public static string CreatePackage(string path,
            string file, string postfix, int milliSecAgo)
        {
            if (string.IsNullOrEmpty(path))
                throw new Exception("missing path");

            if (string.IsNullOrEmpty(file))
                throw new Exception("missing file");

            if (string.IsNullOrEmpty(postfix))
                throw new Exception("missing postfix");

            if (!path.EndsWith(@"\"))
                path += @"\";

            if (!postfix.StartsWith("."))
                postfix = "." + postfix;

            return RecordPackageController.Instance.Package(path, file, postfix, milliSecAgo);
        }

        /// <summary>
        /// 直接追加
        /// </summary>
        public static void Append(string fullfilename, string content)
        {
            if (string.IsNullOrEmpty(fullfilename))
                return;

            string path = StringFilter.GetFilePath(fullfilename);

            if (string.IsNullOrEmpty(path))
                return;

            string filename = StringFilter.GetFileName(fullfilename);

            if (string.IsNullOrEmpty(filename))
                return;

            //10-01-01 返回不带。 bug

            string surfix = "." + StringFilter.GetFileSuffix(fullfilename);

            if (string.IsNullOrEmpty(surfix))
                return;

            filename = filename.Replace(surfix, "");

            Append(path, filename, surfix, content);
        }

        /// <summary>
        /// surfix 需要带。
        /// </summary>
        /// <param name="path"></param>
        /// <param name="filename"></param>
        /// <param name="surfix"></param>
        /// <param name="content"></param>
        public static void Append(string path, string filename, string surfix, string content)
        {
            if (string.IsNullOrEmpty(path))
                return;

            if (string.IsNullOrEmpty(filename))
                return;

            if (string.IsNullOrEmpty(surfix))
                return;

            if (string.IsNullOrEmpty(content))
                return;

            SingletonLogger.Append(path, filename, surfix, content);
        }

        /// <summary>
        /// 直接追加
        /// </summary>
        public static void Write(string fullfilename, string content)
        {
            if (string.IsNullOrEmpty(fullfilename))
                return;

            string path = StringFilter.GetFilePath(fullfilename);

            if (string.IsNullOrEmpty(path))
                return;

            string filename = StringFilter.GetFileName(fullfilename);

            if (string.IsNullOrEmpty(filename))
                return;

            //10-01-01 返回不带。 bug

            string surfix = "." + StringFilter.GetFileSuffix(fullfilename);

            if (string.IsNullOrEmpty(surfix))
                return;

            filename = filename.Replace(surfix, "");

            Write(path, filename, surfix, content);
        }

        /// <summary>
        /// 直接写入
        /// </summary>
        /// <param name="fullfilename"></param>
        /// <param name="content"></param>
        public static void Write(string fullfilename, byte[] content)
        {
            if (string.IsNullOrEmpty(fullfilename))
                return;

            if (content == null || content.Length == 0)
                return;

            SingletonLogger.Write(fullfilename, content);
        }

        /// <summary>
        /// surfix 需要带。
        /// </summary>
        /// <param name="path"></param>
        /// <param name="filename"></param>
        /// <param name="surfix"></param>
        /// <param name="content"></param>
        public static void Write(string path, string filename, string surfix, string content)
        {
            if (string.IsNullOrEmpty(path))
                return;

            if (string.IsNullOrEmpty(filename))
                return;

            if (string.IsNullOrEmpty(surfix))
                return;

            if (string.IsNullOrEmpty(content))
                return;

            SingletonLogger.Write(path, filename, surfix, content);
        }
    }
}
